import {login,wxLogin,isHasPhone,consumerInfo,lotteryCount} from '@/api/api.js'
export default {
	name:"mine",
	data(){
		return{
			userInfo:{
				balance:0,
				kaCoin:0,
				couponNum:0
			},
			modalName:null, //对话框Modal，底部bottomModal
			count:null,
		}
	},
	onLoad(){
		
	},
	onShow(){
		const Authorization = uni.getStorageSync('Authorization');
		if(Authorization){
			this.getConsumerInfo();
			this.getLotteryCount();
		}else{
			this.getUserInfo();
		}
	},
	methods:{
		goDeposit(money){
			uni.navigateTo({
				url:"/pages/mine/deposit/deposit?money="+money
			})
		},
		getLotteryCount(){
			lotteryCount().then(res=>{
				if(res.code==200&&res.data>-1){
					this.count = res.data
				}
			})
		},
		goPrize(){
			uni.navigateTo({
				url:"/pages/mine/prize/prize"
			})
		},
		goLuckDraw(){
			if(this.count){
				uni.navigateTo({
					url:"/pages/index/luckDraw/luckDraw"
				})
			}else{
				uni.showToast({
					title:"暂无抽奖次数,入住后再试试",
					icon:"none"
				})
			}
		},
		goInvite(){
			if(uni.userInfo){
				uni.navigateTo({
					url:"/pages/mine/invite/invite"
				})
			}else{
				uni.showToast({
					title:'请先登录',
					icon:'none'
				})
			}
			
		},
		goHotelOrder(){
			uni.navigateTo({
				url:"/pages/mine/orderList/orderList"
			})
		},
		gokeaidou(kaCoin){
			uni.navigateTo({
				url:"/pages/mine/keaidou/keaidou?kaCoin="+kaCoin
			})
		},
		getuserinfo(e){
			this.userInfo={
				...e.detail.userInfo,
				balance:0,
				kaCoin:0,
				couponNum:0
			}
			this.getUserInfo();
		},
		goCoupon(){
			uni.navigateTo({
				url:"/pages/mine/coupon/coupon",
				fail(e) {
					console.log(e);
				}
			})
		},
		isHavePhoneNumber(){
			isHasPhone({phone:null}).then(res=>{
				if(res.data==1){
					this.modalName='Modal';
				}else if(res.data){
					uni.showModal({
						title:"您是否还在使用"+res.data+"手机号，如果更换手机号，您将无法收到我们的服务信息",
						cancelText:'没有',
						confirmText:'换过',
						success: (e) => {
							if(e.confirm){
								this.modalName='Modal'
							}
						}
					})
				}
			})
		},
		getphonenumber(e){
			if(e.detail.errMsg=='getPhoneNumber:ok'){
				isHasPhone(e.detail).then(res=>{
					if(res.code==200){
						uni.showToast({
							icon:'success',
							title:'授权成功',
							duration:1500
						})
						this.modalName=null;
						this.getConsumerInfo();
					}
				})
			}
		},
		getUserInfo(){
			uni.login({
				success:(e)=>{
					wxLogin(e).then(res=>{
						uni.getUserInfo({
							withCredentials:true,
							success:(e)=>{
								login({...e,sessionKey:res.data.session_key}).then(res=>{
									this.userInfo=res.data;
									uni.userInfo = res.data;
									if(!this.userInfo.phone){
										this.modalName = 'Modal'
									}
								});
							}
						})
					})
				}
			})
		},
		hideModal(){
			this.modalName=null;
		},
		getConsumerInfo(){
			consumerInfo().then(res=>{
				if(res.code==200){
					this.userInfo = res.data;
					uni.userInfo = res.data;
					if(!this.userInfo.phone){
						this.modalName = 'Modal'
					}
				}
			})
		}
	}
}